<template>
  <section>
    <el-form label-width="80px">
      <el-form-item label="名称">
        <el-input size="mini" v-model="tactics.name" style="width: 100px"/>
      </el-form-item>
      <el-form-item label="描述">
        <el-input size="mini" v-model="tactics.description" style="width: 200px"/>
      </el-form-item>
      <el-form-item label="指标" v-if="tactics.id">
        <edit-index :tacticsId="tactics.id"/>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="success" @click="confirmBtn(tactics)">{{tactics.id?'保存':'新建'}}</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
  import editIndex from './tacticsIndex/index'
  import errHandler from '@/assets/errHandler.js'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  export default {
    name: "editTactics",
    components:{editIndex},
    props:['tactics'],
    data(){
      return{
      }
    },
    created(){
    },
    methods:{
      ...mapActions({
        fetch: 'fetch'
      }),
      confirmBtn(tactics){
        let data=tactics
        if(tactics.id){
          data.id=tactics.id
          this.updateTactics(data)
        }
        else this.createTactics(data)
      },
      updateTactics(data){
        this.fetch({name:"updateTactics",data})
          .then(res=>{
            console.log('更新成功-------',res)
            this.$emit('cancelDialog')
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      createTactics(data){
        this.fetch({name:"createTactics",data})
          .then(res=>{
            console.log('创建成功-------',res)
            this.tactics.id=res.id
            // this.$emit('cancelDialog')
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      }
    }
  }
</script>

<style scoped>

</style>
